#!/bin/sh

if test "$1" != ""
then
	dd=$1
else
	dd=1
fi

mysql -uwebsis -pwebsis -h192.168.20.8 -P 4000 spider_worker_v1 -e 'SELECT kwid,keyword from t_auto_keyword' > /root/keyword.txt
mysql -uwebsis -pwebsis -h192.168.20.8 -P 4000 spider_worker_v1 -e 'SELECT domain from t_auto_forum_blog' > /root/site.txt

cat site.txt | while read line1
do
	arr=($line1)
	domain=${arr[0]}
	if test "${domain}" = "domain"
	then
		continue
	fi
#	echo ${domain}
	sleep 1

	n=0
	cat keyword.txt | while read line
	do
	array=($line)
	kwid=${array[0]}
	keyword=${array[1]}
	if test "${n}" = "0"
	then
		n=1
		continue;
	fi

	#echo ${kwid} ${keyword}
	second=`date +%s`
	tm=`date '+%Y-%m-%d %H:%M:%S'`
	sql="INSERT INTO t_spider_task SELECT ${second}*1000+${kwid}, 1008, '${keyword}', '${tm}', 0, DATE(DATE_SUB(NOW(), INTERVAL ${dd} DAY)), DATE(DATE_SUB(NOW(), INTERVAL ${dd} DAY)), 1000, '${domain}', 0, NULL FROM dual;" 
	echo ${sql}
	mysql -uwebsis -pwebsis -h192.168.20.8 -P 4000 spider_worker_v1 -e "${sql}" >>/root/task.log
	done
done
sleep 1

sql="INSERT INTO t_spider_task SELECT UNIX_TIMESTAMP()*1000+kwid, 1008, keyword, NOW(), 0, DATE(DATE_SUB(NOW(), INTERVAL ${dd} DAY)), DATE(DATE_SUB(NOW(), INTERVAL ${dd} DAY)), 1000, NULL, 0, NULL FROM t_auto_keyword;"
mysql -uwebsis -pwebsis -h192.168.20.8 -P 4000 spider_worker_v1 -e "${sql}" >>/root/task.log

